package com.leyou.auth.config;

import com.leyou.auth.task.PrivilegeTokenHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @package com.leyou.auth.config
 * @description: 获取token请求头
 * @author: 许超越
 * @date 2019/7/9 22:05
 * @version: V1.0
 */
@Configuration
public class FeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor(JwtProperties prop, PrivilegeTokenHolder tokenHolder) {
        return new PrivilegeInterceptor(prop, tokenHolder);
    }

    private class PrivilegeInterceptor implements RequestInterceptor {

        private JwtProperties prop;

        private PrivilegeTokenHolder tokenHolder;

        public PrivilegeInterceptor(JwtProperties prop, PrivilegeTokenHolder tokenHolder) {
            this.prop = prop;
            this.tokenHolder = tokenHolder;
        }

        @Override
        public void apply(RequestTemplate template) {
            template.header(prop.getAppTokenProperties().getHeaderName(), tokenHolder.getToken());
        }
    }
}
